'
' Copyright (c) 2004-2005
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Bring2mind.DNN.Modules.Guestbook.Business

Namespace Bring2mind.DNN.Modules.Guestbook.Controls

 Public MustInherit Class Guestbook
  Inherits ModuleBase

#Region " Controls "
  Protected WithEvents dgEntries As System.Web.UI.WebControls.DataGrid
  Protected WithEvents cmdAdd As System.Web.UI.WebControls.HyperLink
  Protected WithEvents cmdEdit As System.Web.UI.WebControls.HyperLink
  Protected WithEvents cmdDelete As System.Web.UI.WebControls.LinkButton
  Protected WithEvents cmdBack As System.Web.UI.WebControls.HyperLink
  Protected WithEvents lblBody As System.Web.UI.WebControls.Label
  Protected WithEvents lblDateSubmitted As System.Web.UI.WebControls.Label
  Protected WithEvents lblUserName As System.Web.UI.WebControls.Label
  Protected WithEvents pnlDetails As System.Web.UI.WebControls.Panel
  Protected WithEvents imgPicture As System.Web.UI.WebControls.Image
#End Region

#Region " Variables "
  Dim entryId As Integer
#End Region

#Region " Event Handlers "
  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   Try

    If Not (Request.Params("EntryId") Is Nothing) Then
     entryId = Int32.Parse(Request.Params("EntryId"))
    Else
     entryId = -1
    End If

    If Not Page.IsPostBack Then
     cmdAdd.NavigateUrl = EditUrl("Edit")
     cmdAdd.Visible = Me.ModuleSettings.CanAdd
     If entryId = -1 Then
      pnlDetails.Visible = False
      dgEntries.PageSize = Me.ModuleSettings.PageSize
      Me.ViewState("PageIndex") = 0
      cmdEdit.Visible = False
      cmdDelete.Visible = False
      cmdBack.Visible = False
      BindList()
     Else
      dgEntries.Visible = False
      cmdBack.NavigateUrl = NavigateURL()
      Dim objEntries As New EntriesController
      Dim objEntry As New EntriesInfo
      objEntry = objEntries.GetEntries(entryId, ModuleId)
      If Not objEntry Is Nothing Then
       lblBody.Text = objEntry.Body
       lblDateSubmitted.Text = objEntry.DateSubmitted.ToString(Me.ModuleSettings.DateFormat)
       lblUserName.Text = objEntry.UserName
       If Not Convert.ToString(objEntry.Picture) = "" And Me.ModuleSettings.AllowPictures Then
        imgPicture.ImageUrl = PicturePath & objEntry.Picture
       Else
        imgPicture.Visible = False
       End If
       cmdEdit.NavigateUrl = EditUrl("EntryId", entryId.ToString, "Edit")
       cmdDelete.Visible = Me.IsEditable Or Me.ModuleSettings.CanApprove
       cmdEdit.Visible = Me.IsEditable Or Me.ModuleSettings.CanApprove
      Else
       cmdEdit.Visible = False
       cmdDelete.Visible = False
      End If
     End If
    Else
     'its a postback
    End If

   Catch exc As Exception
    ProcessModuleLoadException(Me, exc)
   End Try
  End Sub

  Public Sub EntryGridPageCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)

   Me.ViewState("PageIndex") = e.NewPageIndex
   dgEntries.Controls.Clear()
   BindList()

  End Sub

  Private Sub dgEntries_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgEntries.ItemCommand
   Select Case e.CommandName
    Case "approve"
     If Not Me.ModuleSettings.CanApprove Then Exit Sub
     Dim SelectedEntryId As Integer = Convert.ToInt32(e.CommandArgument)
     Dim objEntries As New EntriesController
     Dim objEntry As New EntriesInfo
     objEntry = objEntries.GetEntries(SelectedEntryId, ModuleId)
     If Not objEntry Is Nothing Then
      objEntry.IsApproved = True
      objEntries.UpdateEntries(objEntry)
      Me.Response.Redirect(Me.Request.RawUrl)
     End If
   End Select
  End Sub

  Private Sub cmdDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
   If entryId <> -1 Then
    Dim objEntries As New EntriesController
    Dim objEntry As EntriesInfo = objEntries.GetEntries(entryId, ModuleId)
    If Not objEntry Is Nothing Then
     If Convert.ToString(objEntry.Picture) <> "" Then
      IO.File.Delete(PictureMapPath & objEntry.Picture)
      IO.File.Delete(ThumbnailMapPath & objEntry.Picture)
     End If
     objEntries.DeleteEntries(entryId)
    End If
    Me.Response.Redirect(NavigateURL, True)
   End If
  End Sub
#End Region

#Region " Private Methods "
  Private Sub BindList()
   DotNetNuke.Services.Localization.Localization.LocalizeDataGrid(dgEntries, Me.LocalResourceFile)

   Dim dv As New DataView(DotNetNuke.Common.ConvertDataReaderToDataTable(Data.DataProvider.Instance().GetEntriesByModules(ModuleId, Me.ModuleSettings.CanApprove)))
   With dv
    .Sort = "DateSubmitted DESC"
   End With

   With dgEntries
    .CurrentPageIndex = Convert.ToInt32(Me.ViewState("PageIndex"))
    .DataSource = dv
    .DataBind()
    If .AllowPaging Then
     If dv.Count <= .PageSize Then
      .AllowPaging = False
     Else
      .AllowPaging = True
     End If
    End If
   End With

  End Sub
#End Region

#Region " Web Form Designer Generated Code "

  'This call is required by the Web Form Designer.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

  End Sub

  'NOTE: The following placeholder declaration is required by the Web Form Designer.
  'Do not delete or move it.
  Private designerPlaceholderDeclaration As System.Object

  Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
   'CODEGEN: This method call is required by the Web Form Designer
   'Do not modify it using the code editor.
   InitializeComponent()
  End Sub

#End Region

 End Class

End Namespace
